<template>
  <!--<div>添加案件 </div>-->
  <el-dialog
	v-loading.fullscreen.lock="fullLoading"
	element-loading-text="拼命加载中"
    title="新增报案"
    :close-on-click-modal="false"
    :visible.sync="visible"
    fullscreen
  >
    <el-form
      label-width="140px"
      :model="dataForm"
      :rules="rules"
      ref="dataForm"
    >
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>出险人员信息</span>
        </div>
        <el-row>
          <el-col :span="8">
            <el-form-item label="姓名:" prop="insuredInfo.insuredName">
              <el-input
                v-model="dataForm.insuredInfo.insuredName"
                placeholder=""
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="证件类型:" prop="insuredInfo.certificateType">
              <el-select
                v-model="dataForm.insuredInfo.certificateType"
                @change="calIdCard"
              >
                <el-option
                  v-for="item in Consts.certificateTypeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="证件号码:" prop="insuredInfo.certificateNo">
              <el-input
                v-model="dataForm.insuredInfo.certificateNo"
                placeholder=""
                @change="calIdCard"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="年龄:" prop="insuredInfo.age">
              <el-input
                v-model.number="dataForm.insuredInfo.age"
                placeholder=""
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="性别:" prop="insuredInfo.gender">
              <el-radio-group v-model="dataForm.insuredInfo.gender">
                <el-radio label="1">男</el-radio>
                <el-radio label="0">女</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="出生日期:" prop="insuredInfo.birthday">
              <el-date-picker
                v-model="dataForm.insuredInfo.birthday"
                type="date"
                value-format="yyyy-MM-dd HH:mm:ss"
                format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="集团名称:" prop="insuredInfo.corpName">
              <el-input
                v-model="dataForm.insuredInfo.corpName"
                placeholder=""
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系电话:" prop="insuredInfo.phone">
              <el-input
                v-model="dataForm.insuredInfo.phone"
                placeholder=""
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
		<el-row>
			<el-col :span="16">
				<el-form-item label="被保险人居住地:">
					<el-select v-model="dataForm.insuredInfo.provinceCode" placeholder="请选择省" filterable :filter-method='pinyinMatch' clearable @change='change'>
						<el-option v-for="item in Area" :key="item.value" :label="item.label" :value="item.value"></el-option>
					</el-select>
					<el-select v-model="dataForm.insuredInfo.cityCode" placeholder="请选择市" filterable :filter-method='pinyinMatch1' @change='change1' @focus='focus'>
						<el-option v-for="item in cityQuery" :key="item.value" :label="item.label" :value="item.value"></el-option>
					</el-select>
					<el-select v-model="dataForm.insuredInfo.countyCode" placeholder="请选择区" filterable :filter-method='pinyinMatch2' @change='change2' @focus='focus1'>
						<el-option v-for="item in areaQuery" :key="item.value" :label="item.label" :value="item.value"></el-option>
					</el-select>
				</el-form-item>
			</el-col>
		</el-row>
		<el-row>
			<el-col :span="8">
			  <el-form-item label="详细地址:" prop="insuredInfo.address">
			    <el-input
			      v-model="dataForm.insuredInfo.address"
			      placeholder="请输入详细地址"
			    ></el-input>
			  </el-form-item>
			</el-col>
		</el-row>
      </el-card>
      <el-card class="box-card" style="margin-top: 20px">
        <div slot="header" class="clearfix">
          <span>保单信息</span>
        </div>
        <el-row>
          <el-col :span="8">
            <el-form-item label="区域:" prop="policyInfo.region">
              <el-input
                v-model="dataForm.policyInfo.region"
                placeholder=""
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="项目编号:" prop="policyInfo.projectId">
              <el-input
                v-model="dataForm.policyInfo.projectId"
                placeholder=""
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="项目名称:" prop="policyInfo.projectName">
              <el-input
                v-model="dataForm.policyInfo.projectName"
                placeholder=""
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="保单号:" prop="policyInfo.policyNo">
              <el-input
                v-model="dataForm.policyInfo.policyNo"
                placeholder=""
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="承保公司:" prop="policyInfo.underwriteCorp">
              <el-input
                v-model="dataForm.policyInfo.underwriteCorp"
                placeholder=""
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="险种名称:" prop="policyInfo.insuranceProduct">
              <el-input
                v-model="dataForm.policyInfo.insuranceProduct"
                placeholder=""
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="保障期间:" prop="policyInfo.effectTimes">
              <el-date-picker
                type="daterange"
                value-format="yyyy-MM-dd HH:mm:ss"
                v-model="dataForm.policyInfo.effectTimes"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                style="max-width: 238px"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="保障状态:" prop="policyInfo.insuranceStatus">
              <el-select v-model="dataForm.policyInfo.insuranceStatus">
                <el-option
                  v-for="item in Consts.insuranceStatusList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="保额:" prop="policyInfo.insuredAmountYuan">
              <el-input
                v-model="dataForm.policyInfo.insuredAmountYuan"
                placeholder=""
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="投保人:" prop="policyInfo.policyholderName">
              <el-input
                v-model="dataForm.policyInfo.policyholderName"
                placeholder=""
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="被保人:" prop="policyInfo.insuredName">
              <el-input
                v-model="dataForm.policyInfo.insuredName"
                placeholder=""
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="人员类别:" prop="policyInfo.personnelSort">
              <el-select v-model="dataForm.policyInfo.personnelSort">
                <el-option
                  v-for="item in Consts.personnelSortList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item
              label="是否外包:"
              prop="policyInfo.outsourcingFlag"
              required
            >
              <el-select v-model="dataForm.policyInfo.outsourcingFlag">
                <el-option
                  v-for="item in Consts.outsourcingFlagList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否欠费:" prop="policyInfo.payStatus">
              <el-select v-model="dataForm.policyInfo.payStatus">
                <el-option
                  v-for="item in Consts.payStatusList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-card>
      <el-card class="box-card" style="margin-top: 20px">
        <div slot="header" class="clearfix">
          <span>案件信息</span>
        </div>
        <el-row>
          <el-col :span="8">
            <el-form-item label="报案人:" prop="claimCase.reporterName">
              <el-input
                v-model="dataForm.claimCase.reporterName"
                placeholder=""
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="报案人联系电话:"
              prop="claimCase.reporterPhone"
            >
              <el-input
                v-model="dataForm.claimCase.reporterPhone"
                placeholder=""
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="报案时间:" prop="claimCase.reportTime">
              <el-date-picker
                v-model="dataForm.claimCase.reportTime"
                type="datetime"
                placeholder="选择日期时间"
                value-format="yyyy-MM-dd HH:mm:ss"
                format="yyyy-MM-dd HH:mm:ss"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="出险时间:" prop="claimCase.accidentTime">
              <el-date-picker
                v-model="dataForm.claimCase.accidentTime"
                type="datetime"
                placeholder="选择日期时间"
                value-format="yyyy-MM-dd HH:mm:ss"
                format="yyyy-MM-dd HH:mm:ss"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="事故类型:" prop="claimCase.accidentType">
              <el-select v-model="dataForm.claimCase.accidentType">
                <el-option
                  v-for="item in Consts.accidentTypeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="估损金额:" prop="claimCase.estimatedLossYuan">
              <el-input
                v-model="dataForm.claimCase.estimatedLossYuan"
                placeholder=""
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
		<el-row>
			<el-col :span="16">
				<el-form-item label="事故发生地:">
					<el-select v-model="dataForm.claimCase.provinceCodeAccident" placeholder="请选择省" filterable :filter-method='pinyinMatchAccident' clearable @change='changeAccident'>
						<el-option v-for="item in Area" :key="item.value" :label="item.label" :value="item.value"></el-option>
					</el-select>
					<el-select v-model="dataForm.claimCase.cityCodeAccident" placeholder="请选择市" filterable :filter-method='pinyinMatch1Accident' @change='change1Accident' @focus='focusAccident'>
						<el-option v-for="item in cityQueryAccident" :key="item.value" :label="item.label" :value="item.value"></el-option>
					</el-select>
					<el-select v-model="dataForm.claimCase.countyCodeAccident" placeholder="请选择区" filterable :filter-method='pinyinMatch2Accident' @change='change2Accident' @focus='focus1Accident'>
						<el-option v-for="item in areaQueryAccident" :key="item.value" :label="item.label" :value="item.value"></el-option>
					</el-select>
				</el-form-item>
			</el-col>
		</el-row>
		<el-row>
			<el-col :span="8">
			  <el-form-item label="详细地址:" prop="claimCase.accidentAddress">
			    <el-input
			      v-model="dataForm.claimCase.accidentAddress"
			      placeholder="请输入详细地址"
			    ></el-input>
			  </el-form-item>
			</el-col>
		</el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="受伤部位:" prop="claimCase.injuredPart">
              <el-input
                v-model="dataForm.claimCase.injuredPart"
                placeholder=""
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="事故性质:" prop="claimCase.accidentNature">
              <el-select v-model="dataForm.claimCase.accidentNature">
                <el-option
                  v-for="item in Consts.accidentNatureList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="事故责任:" prop="claimCase.accidentLiability">
              <el-select v-model="dataForm.claimCase.accidentLiability">
                <el-option
                  v-for="item in Consts.accidentLiabilityList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="案件编号:" prop="claimCase.caseNo">
              <el-input
                v-model="dataForm.claimCase.caseNo"
                placeholder="请输入案件编号" readonly
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item
              label="出险描述:"
              prop="claimCase.accidentDescription"
            >
              <el-input
                type="textarea"
                :rows="3"
                placeholder="请输入事故发生时间、地点、原因及经过"
                v-model="dataForm.claimCase.accidentDescription"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item
            label="添加事故现场照片:"
            prop="accidentPicUrlUploadFlag"
          >
            <el-upload
              class="upload-demo"
			  ref="uploadDemo"
              drag
              action="#"
              :data="accidentPicUrl"
              :file-list="accidentPicUrlFileList"
              :http-request="submitFileUpload"
              :on-remove="handleFileRemove"
              multiple
            >
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">
                将文件拖到此处，或<em>点击上传</em>
              </div>
            </el-upload>
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item
              label-width="200px"
              label="向保险公司报案时间:"
              prop="claimCase.sendTime"
            >
              <el-date-picker
                v-model="dataForm.claimCase.sendTime"
                type="datetime"
                placeholder="选择日期时间"
                value-format="yyyy-MM-dd HH:mm:ss"
                format="yyyy-MM-dd HH:mm:ss"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="center">
          <el-form-item>
            <el-button @click="goBack()">返回</el-button>
            <el-button type="primary" @click="save()" :loading="saveLoading"
              >保存</el-button
            >
          </el-form-item>
        </el-row>
      </el-card>
    </el-form>
  </el-dialog>
</template>

<script>
import { isIdCard, isBirthCard, isMoney, isMobile } from "@/utils/validate";
import { getAddress } from "@/utils/address.js";

export default {
  data() {
    // 自定义校验规则
    var validateMobile = (rule, value, callback) => {
      if(!value){
        callback()
      }
      if (!isMobile(value)) {
        callback(new Error("手机号格式错误"));
      } else {
        callback();
      }
    };
    var validateMoney = (rule, value, callback) => {
      if(!value){
        callback()
      }
      if (!isMoney(value)) {
        callback(new Error("金额格式错误(仅可保留两位小数)"));
      } else {
        callback();
      }
    };
    var validateIdCard = (rule, value, callback) => {
      if(!value){
        callback();
      }
      if (this.dataForm.insuredInfo.certificateType === 1 && !isIdCard(value)) {
        callback(new Error("证件号格式错误"));
      } else if (
        this.dataForm.insuredInfo.certificateType === 2 &&
        !isBirthCard(value)
      ) {
        callback(new Error("证件号格式错误"));
      } else {
        callback();
      }
    };
    var validateAccidentPicUrlFile = (rule, value, callback) => {
      callback();
      /*if (value <= 0) {
        callback(new Error("请添加事故现场照片"));
      } else {
        callback();
      }*/
    };
    return {
	  fullLoading:false,
    accidentPicUrlFileList:[],
	  Area: [],
	  cityQuery: [],
	  cityQueryAccident: [],
	  areaQuery: [],
	  areaQueryAccident: [],
      saveLoading: false,
      accidentPicUrl: {
        attachment: "accidentPicUrl",
        entityType: "claimCase",
      },
      dataForm: {
	    files:[],
        accidentPicUrlUploadFlag: 0,
        insuredInfo: {
          insuredName: "",
          certificateType: 1,
          certificateNo: "",
          age: "",
          gender: "",
          birthday: "",
          address: "",
          phone: "",
          corpName: "",
		  provinceCode: '',
		  provinceName: '',
		  cityCode: '',
		  cityName: '',
		  countyCode: '',
		  countyName: ''
        },
        policyInfo: {
          region: "",
          projectId: "",
          projectName: "",
          policyNo: "",
          underwriteCorp: "",
          insuranceProduct: "",
          startTime: "",
          endTime: "",
          insuranceStatus: "",
          insuredAmountYuan: "",
          policyholderName: "",
          insuredName: "",
          personnelSort: "",
          outsourcingFlag: "",
          payStatus: "",
          effectTimes: "",
        },
        claimCase: {
          reporterName: "",
          reporterPhone: "",
          reportTime: "",
          accidentTime: "",
          accidentAddress: "",
          accidentType: "",
          injuredPart: "",
          accidentNature: "",
          accidentLiability: "",
          accidentDescription: "",
          accidentPicUrl: "",
          sendTime: "",
          caseNo: "",
		  provinceCodeAccident: '',
		  provinceNameAccident: '',
		  cityCodeAccident: '',
		  cityNameAccident: '',
		  countyCodeAccident: '',
		  countyNameAccident: '',
		  estimatedLossYuan:''
        },
      },
      rules: {
        insuredInfo: {
          insuredName: [
            { required: true, message: "请输入姓名", trigger: "blur" },
          ],
          certificateType: [
            { required: true, message: "请选择证件类型", trigger: "change" },
          ],
          certificateNo: [
            { validator: validateIdCard, trigger: "blur" },
            { required: false, message: "请输入证件号", trigger: "blur" },
          ],
          age: [{ required: false, message: "请输入年龄", trigger: "blur" }],
          gender: [
            { required: true, message: "请选择性别", trigger: "change" },
          ],
          birthday: [
            { required: false, message: "请选择日期", trigger: "change" },
          ],
          corpName: [
            { required: false, message: "请输入集团名称", trigger: "blur" },
          ],
          address: [{ required: false, message: "请输入地址", trigger: "blur" }],
          phone: [
            { validator: validateMobile, trigger: "blur" },
            { required: false, message: "请输入联系电话", trigger: "blur" },
          ],
        },
        policyInfo: {
          region: [{ required: false, message: "请输入区域", trigger: "blur" }],
          projectId: [
            { required: false, message: "请输入项目编号", trigger: "blur" },
          ],
          projectName: [
            { required: false, message: "请输入项目名称", trigger: "blur" },
          ],
          policyNo: [
            { required: false, message: "请输入保单号", trigger: "blur" },
          ],
          underwriteCorp: [
            { required: false, message: "请输入承保公司", trigger: "blur" },
          ],
          insuranceProduct: [
            { required: false, message: "请输入险种名称", trigger: "blur" },
          ],
          effectTimes: [
            { required: false, message: "请选择时间", trigger: "change" },
          ],
          insuredAmountYuan: [
            { validator: validateMoney, trigger: "blur" },
            { required: false, message: "请输入保额", trigger: "blur" },
          ],
          policyholderName: [
            { required: true, message: "请输入投保人", trigger: "blur" },
          ],
          insuranceStatus: [
            { required: false, message: "请选择保障状态", trigger: "change" },
          ],
          personnelSort: [
            { required: true, message: "请选择人员类别", trigger: "change" },
          ],
          outsourcingFlag: [
            { required: true, message: "请选择是否外包", trigger: "change" },
          ],
          insuredName: [
            { required: true, message: "请输入被保人", trigger: "blur" },
          ],
          payStatus: [
            {
              required: false,
              message: "请选择是否欠费",
              trigger: "change",
            },
          ],
        },
        claimCase: {
          reporterName: [
            { required: true, message: "请输入报案人", trigger: "blur" },
          ],
          reporterPhone: [
            { validator: validateMobile, trigger: "blur" },
            { required: true, message: "请输入联系电话", trigger: "blur" },
          ],
          reportTime: [
            { required: true, message: "请选择报案时间", trigger: "change" },
          ],
          accidentTime: [
            { required: true, message: "请选择出险时间", trigger: "change" },
          ],
          accidentAddress: [
            { required: true, message: "请输入事故发生地", trigger: "blur" },
          ],
          accidentType: [
            { required: true, message: "请选择事故类型", trigger: "change" },
          ],
          injuredPart: [
            { required: false, message: "请输入受伤部位", trigger: "blur" },
          ],
          accidentNature: [
            { required: false, message: "请选择事故性质", trigger: "change" },
          ],
          accidentLiability: [
            { required: false, message: "请选择事故责任", trigger: "change" },
          ],
          caseNo: [
            { required: true, message: "请输入案件编号", trigger: "blur" },
          ],
          accidentDescription: [
            { required: true, message: "请输入出险描述", trigger: "blur" },
          ],
          sendTime: [
            { required: true, message: "请选择时间", trigger: "change" },
          ],
		  estimatedLossYuan: [
		    { validator: validateMoney, trigger: "blur" },
		    { required: false, message: "请输入估损金额", trigger: "blur" },
		  ],
        },
        accidentPicUrlUploadFlag: [
          {
            required: false,
            validator: validateAccidentPicUrlFile,
            trigger: "change",
          },
        ],
      },
      dataList: [],
      dataListLoading: false,
      visible: false,
      files: [],
    };
  },
  components: {
    // AddOrUpdate
  },
  activated() {
    // this.visible = true;
    // this.$nextTick(() => {
    //   this.$refs["dataForm"].resetFields();
    // });
  },
  created: function() {
		this.Area = getAddress()
		console.log('this.cityQuery=========', this.Area)
  },
  methods: {
    // 获取数据列表
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
		this.dataForm.insuredInfo.provinceCode=''
		this.dataForm.insuredInfo.provinceName=''
		this.dataForm.insuredInfo.cityCode=''
		this.dataForm.insuredInfo.cityName=''
		this.dataForm.insuredInfo.countyCode=''
		this.dataForm.insuredInfo.countyName=''
		this.dataForm.claimCase.provinceCodeAccident=''
		this.dataForm.claimCase.provinceNameAccident=''
		this.dataForm.claimCase.cityCodeAccident=''
		this.dataForm.claimCase.cityNameAccident=''
		this.dataForm.claimCase.countyCodeAccident=''
		this.dataForm.claimCase.countyNameAccident=''
		this.$refs["uploadDemo"].clearFiles();
		this.dataForm.files = [];
		this.files = [];
		this.$forceUpdate()
		this.makeCaseNo()
      });
    },
    goBack: function () {
      this.visible = false;
    },
    save: function () {
      this.saveLoading = true;
	  console.log("this.dataForm", this.dataForm)
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.dataForm.policyInfo.startTime =
            this.dataForm.policyInfo.effectTimes[0];
          this.dataForm.policyInfo.endTime =
            this.dataForm.policyInfo.effectTimes[1];
          this.dataForm.files = this.files;
          console.log("this.dataForm", this.dataForm);
          this.$http({
            url: this.$http.adornUrl("/claim/claimcase/save"),
            method: "post",
            data: this.$http.adornData(this.dataForm),
          }).then(({ data }) => {
            this.saveLoading = false;
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        } else {
          this.saveLoading = false;
        }
      });
    },
    submitFileUpload: function (req) {
		this.fullLoading = true;
      console.log(req);
      let that = this;
      let reader = new FileReader();
      reader.readAsDataURL(req.file);
      reader.onload = function () {
        let newFileName = that.renameFile(req.file.name, req.file.uid);
        that
          .$http({
            url: that.$http.adornUrl("/migrantWorkers/file/upload"),
            method: "post",
            data: JSON.stringify({ file: reader.result, path: newFileName }),
          })
          .then(({ data }) => {
			  that.fullLoading = false;
            if (data && data.code === 0) {
              let fileParams = {
                fileName: newFileName,
                attachment: req.data.attachment,
                fileType: req.file.type,
                filePath: newFileName,
                entityType: req.data.entityType,
                description: "",
                uid: req.file.uid,
              };
              console.log("gggg", fileParams);
              that.files.push(fileParams);
              that.modifyUploadFlag("upload", req.data.attachment);
			  that.$message.success("上传成功");
            } else if (data && data.msg) {
              that.$message.error(data.msg);
            }
          }).catch(error => {
          // 失败：统一处理错误
          that.$message.error("上传文件异常");
          that.fullLoading = false;
          console.error("上传文件error:", error);
          that.accidentPicUrlFileList = that.accidentPicUrlFileList.filter(item => {
            if (item.uid === req.file.uid) {
              return false;
            }
            return true;
          });
        });
      };
    },
    handleFileRemove: function (req) {
      // 删除文件时调用阿里云接口对文件进行删除
      let that = this;
      let filePath = that.renameFile(req.raw.name, req.raw.uid);
      that
        .$http({
          url: that.$http.adornUrl("/migrantWorkers/file/delete"),
          method: "post",
          data: JSON.stringify({ path: filePath }),
        })
        .then(({ data }) => {
          if (data && data.code === 0) {
            that.files.some((item, i) => {
              if (item.uid === req.raw.uid) {
                that.modifyUploadFlag("delete", item.attachment);
                that.files.splice(i, 1);
              }
            });
			that.$message.success("文件删除成功");
          } else if (data && data.msg) {
            that.$message.error(data.msg);
          }
        });
    },
    modifyUploadFlag: function (oper, attachment) {
      let flagVal = 0;
      let that = this;
      switch (oper) {
        case "upload":
          flagVal = 1;
          break;
        case "delete":
          flagVal = -1;
          break;
        default:
          flagVal = 0;
          break;
      }
      switch (attachment) {
        case "accidentPicUrl":
          that.dataForm.accidentPicUrlUploadFlag += flagVal;
          break;
      }
    },
    renameFile: function (originFileName, fileUID) {
      if (originFileName == null || originFileName === undefined) {
        return null;
      }
      let fileNameSplit = originFileName.split(".");
      let fileName = "";
      for (let i = 0; i < fileNameSplit.length - 1; i++) {
        if (i < fileNameSplit.length - 2) {
          fileName = fileName + fileNameSplit[i] + ".";
        } else {
          fileName = fileName + fileNameSplit[i];
        }
      }
      // 跟文件名加上时间
      fileName = fileName + "_" + fileUID;
      let fileExtendName = fileNameSplit[fileNameSplit.length - 1];
      return fileName + "." + fileExtendName;
    },
    calIdCard: function () {
      let idCard = this.dataForm.insuredInfo.certificateNo;
      if (!idCard || this.dataForm.insuredInfo.certificateType !== 1) {
        return;
      }
      let sex = null;
      let birth = null;
      let myDate = new Date();
      let month = myDate.getMonth() + 1;
      let day = myDate.getDate();
      let age = 0;

      if (idCard.length === 18) {
        age = myDate.getFullYear() - idCard.substring(6, 10) - 1;
        sex = idCard.substring(16, 17);
        birth =
          idCard.substring(6, 10) +
          "-" +
          idCard.substring(10, 12) +
          "-" +
          idCard.substring(12, 14);
        if (
          idCard.substring(10, 12) < month ||
          (idCard.substring(10, 12) === month &&
            idCard.substring(12, 14) <= day)
        ) {
          age++;
        }
      }
      if (idCard.length === 15) {
        age = myDate.getFullYear() - idCard.substring(6, 8) - 1901;
        sex = idCard.substring(13, 14);
        birth =
          "19" +
          idCard.substring(6, 8) +
          "-" +
          idCard.substring(8, 10) +
          "-" +
          idCard.substring(10, 12);
        if (
          idCard.substring(8, 10) < month ||
          (idCard.substring(8, 10) === month && idCard.substring(10, 12) <= day)
        ) {
          age++;
        }
      }
      // 性别代码 1代表男，0代表女，暂时不涉及其他类型性别
      if (sex % 2 === 0) {
        sex = "0";
      } else {
        sex = "1";
      }
      if (birth && birth.length === 10) {
        this.dataForm.insuredInfo.birthday = birth + " 00:00:00";
      }
      this.dataForm.insuredInfo.age = age;
      this.dataForm.insuredInfo.gender = sex;
    },
    makeCaseNo: function () {
      this.$http({
            url: this.$http.adornUrl("/claim/claimcase/makeCaseNo"),
            method: "post"
          })
          .then(({ data }) => {
            if (data && data.code === 0) {
              console.log("data",data)
              this.dataForm.claimCase.caseNo = data.data
            } else if (data && data.msg) {
              this.$message.error("案件编号生成失败");
            }
          });
    },
	pinyinMatch(val) {
		if (val) {
			let result = []; //声明一个空数组保存搜索内容
			this.Area.forEach(e => { //循环判断内容和拼音首字母是否匹配
				let m = PinyinMatch.match(e.label, val)
				if (m) {
					result.push(e)
				}
			});
			this.Area = result; //返回匹配的数组
		} else {
			this.Area = getAddress()
		}
	},
	pinyinMatch1(val) {
		if (val) {
			let result = []; //声明一个空数组保存搜索内容
			this.cityQuery.forEach(e => {
				let m = PinyinMatch.match(e.label, val)
				console.log(m)
				if (m) {
					result.push(e)
				}
			});
			this.cityQuery = result; //返回匹配的数组
		} else {
			this.Area.forEach((item) => {
				if (this.dataForm.insuredInfo.provinceCode.label === item.label) {
					this.cityQuery = item.children
					console.log('cityCode', this.cityQuery)
				}
			})
		}
	},
	pinyinMatch2(val) {
		if (val) {
			let result = []; //声明一个空数组保存搜索内容
			this.areaQuery.forEach(e => {
				let m = PinyinMatch.match(e.label, val)
				if (m) {
					result.push(e)
				}
			});
			this.areaQuery = result; //返回匹配的数组
		} else {
			this.cityQuery.forEach((item) => {
				if (this.dataForm.insuredInfo.cityCode.label === item.label) {
					this.areaQuery = item.children
				}
			})
		}
	},
	change(newValue) {
		console.log(newValue)
		this.cityQuery = []
		this.areaQuery = []
		this.dataForm.insuredInfo.cityCode = ''
		this.dataForm.insuredInfo.countyCode = ""
		this.Area = getAddress()
		this.Area.forEach((item) => {
			if (newValue === item.value) {
				this.dataForm.insuredInfo.provinceName = item.label
			}
		})
		console.log(this.dataForm.insuredInfo.provinceName)
	},
	focus() {
		console.log(this.dataForm.insuredInfo.provinceCode)
		this.Area.forEach((item) => {
			if (this.dataForm.insuredInfo.provinceCode === item.value) {
				this.cityQuery = item.children
				console.log('cityCode', this.cityQuery)
			}
		})
	},
	focus1() {
		console.log(this.cityCode)
		this.cityQuery.forEach((item) => {
			if (this.dataForm.insuredInfo.cityCode === item.value) {
				this.areaQuery = item.children
			}
		})
	},
	change1(newValue) {
		console.log(newValue)
		this.areaQuery = []
		this.dataForm.insuredInfo.countyCode = ""
		this.cityQuery.forEach((item) => {
			if (newValue === item.value) {
				this.dataForm.insuredInfo.cityName = item.label
			}
		})
		console.log(this.dataForm.insuredInfo.cityName)
	},
	change2(newValue) {
		this.areaQuery.forEach((item) => {
			if (newValue === item.value) {
				this.dataForm.insuredInfo.countyName = item.label
			}
		})
		console.log(this.dataForm.insuredInfo.countyName)
	},
	pinyinMatchAccident(val) {
		if (val) {
			let result = []; //声明一个空数组保存搜索内容
			this.Area.forEach(e => { //循环判断内容和拼音首字母是否匹配
				let m = PinyinMatch.match(e.label, val)
				if (m) {
					result.push(e)
				}
			});
			this.Area = result; //返回匹配的数组
		} else {
			this.Area = getAddress()
		}
	},
	pinyinMatch1Accident(val) {
		if (val) {
			let result = []; //声明一个空数组保存搜索内容
			this.cityQueryAccident.forEach(e => {
				let m = PinyinMatch.match(e.label, val)
				console.log(m)
				if (m) {
					result.push(e)
				}
			});
			this.cityQueryAccident = result; //返回匹配的数组
		} else {
			this.Area.forEach((item) => {
				if (this.dataForm.claimCase.provinceCodeAccident.label === item.label) {
					this.cityQueryAccident = item.children
					console.log('cityCode', this.cityQuery)
				}
			})
		}
	},
	pinyinMatch2Accident(val) {
		if (val) {
			let result = []; //声明一个空数组保存搜索内容
			this.areaQueryAccident.forEach(e => {
				let m = PinyinMatch.match(e.label, val)
				if (m) {
					result.push(e)
				}
			});
			this.areaQueryAccident = result; //返回匹配的数组
		} else {
			this.cityQueryAccident.forEach((item) => {
				if (this.dataForm.claimCase.cityCodeAccident.label === item.label) {
					this.areaQueryAccident = item.children
				}
			})
		}
	},
	changeAccident(newValue) {
		console.log(newValue)
		this.cityQueryAccident = []
		this.areaQueryAccident = []
		this.dataForm.claimCase.cityCodeAccident = ''
		this.dataForm.claimCase.countyCodeAccident = ""
		this.Area = getAddress()
		this.Area.forEach((item) => {
			if (newValue === item.value) {
				this.dataForm.claimCase.provinceNameAccident = item.label
			}
		})
		console.log(this.dataForm.claimCase.provinceNameAccident)
	},
	focusAccident() {
		console.log(this.dataForm.claimCase.provinceCodeAccident)
		this.Area.forEach((item) => {
			if (this.dataForm.claimCase.provinceCodeAccident === item.value) {
				this.cityQueryAccident = item.children
				console.log('cityCodeAccident', this.cityQueryAccident)
			}
		})
	},
	focus1Accident() {
		console.log(this.cityCode)
		this.cityQueryAccident.forEach((item) => {
			if (this.dataForm.claimCase.cityCodeAccident === item.value) {
				this.areaQueryAccident = item.children
			}
		})
	},
	change1Accident(newValue) {
		console.log(newValue)
		this.areaQueryAccident = []
		this.dataForm.claimCase.countyCodeAccident = ""
		this.cityQueryAccident.forEach((item) => {
			if (newValue === item.value) {
				this.dataForm.claimCase.cityNameAccident = item.label
			}
		})
		console.log(this.dataForm.claimCase.cityNameAccident)
	},
	change2Accident(newValue) {
		this.areaQueryAccident.forEach((item) => {
			if (newValue === item.value) {
				this.dataForm.claimCase.countyNameAccident = item.label
			}
		})
		console.log(this.dataForm.claimCase.countyNameAccident)
	},
  },
};
</script>

<style>
</style>
